package com.lb.activity.sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lb.activity.mapper.RoleMapper;
import com.lb.activity.mapper.UserRoleMapper;
import com.lb.activity.po.Role;
import com.lb.activity.po.RoleExample;
import com.lb.activity.po.UserRole;
import com.lb.activity.po.UserRoleExample;
import com.lb.activity.sys.service.RoleService;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.util.SnowflakeIdWorker;
import com.lb.activity.util.TreeJson;
import com.lb.activity.util.emumdata.DataEnum;
import com.lb.activity.vo.PageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 001030
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    private SnowflakeIdWorker idWorker;

    @Resource
    private RoleMapper roleMapper;

    @Resource
    private UserRoleMapper userRoleMapper;

    /**
     * 天加新的角色
     * @param role
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultUtil addRole(Role role) {
        role.setId(idWorker.nextId());
        role.setStatus(Integer.parseInt(DataEnum.INSERT_STATUS.getStatus()+""));
        int i = roleMapper.insertSelective(role);
        return ResultUtil.ok(i>0);
    }


    @Override
    public ResultUtil roles(PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        RoleExample roleExample = new RoleExample();
        List<Role> roles = roleMapper.selectByExample(roleExample);
        PageInfo<Role> pageInfo = new PageInfo(roles);
        return ResultUtil.build(0,"",Integer.parseInt(pageInfo.getTotal()+""),roles);
    }

    @Override
    public Role getRoleById(Long roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    /**
     * 将role 和 用户和角色的中间表  处理
     * @param userId
     * @return
     */
    @Override
    public List<TreeJson> getRoleTreeJsons(Long userId) {
        //查询中间表数据
        UserRoleExample exampleUserRole = new UserRoleExample();
        UserRoleExample.Criteria criteriaUserRole = exampleUserRole.createCriteria();
        criteriaUserRole.andUseridEqualTo(userId);
        criteriaUserRole.andStatusEqualTo(Integer.parseInt(DataEnum.INSERT_STATUS.getStatus()+""));
        List<UserRole> storeUserRoles = userRoleMapper.selectByExample(exampleUserRole);
        //查询角色列表
        RoleExample example = new RoleExample();
        RoleExample.Criteria criteria = example.createCriteria();
        criteria.andStatusEqualTo(Integer.parseInt(DataEnum.INSERT_STATUS.getStatus()+""));
        List<Role> storeAdminRoles = roleMapper.selectByExample(example);
        List<TreeJson> list = new ArrayList<>();
        for(Role storeAdminRole : storeAdminRoles){
            TreeJson json = new TreeJson();
            json.setId(storeAdminRole.getId()+"");
            json.setName(storeAdminRole.getRolename());
            json.setPId(0L);
            json.setOpen(false);
            for(UserRole storeUserRole : storeUserRoles){
                if(storeAdminRole.getId().equals(storeUserRole.getRoleid())){
                    json.setChecked(true);
                }else {
                    continue;
                }
            }
            list.add(json);
        }
        return list;
    }
}
